package com.all.ui.adapter;

import java.util.Map;

import com.all.ui.R;
import com.all.utils.ImageUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageListViewAdapter extends ListBaseAdapter {

	private Context context=null;
	private static ImageLoader universalimageloader;
	public ImageListViewAdapter(Context context)
	{
		super(context);
		this.context= context;
	}
	@SuppressWarnings("unchecked")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder mViewHolder;
		if(null == convertView){
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.activity_image_listview_item,null);
			mViewHolder = new ViewHolder();
			mViewHolder.iv_icon = (ImageView)convertView.findViewById(R.id.iv_icon);
			mViewHolder.tv_title = (TextView)convertView.findViewById(R.id.tv_title);
			convertView.setTag(mViewHolder);
		}else{
			mViewHolder = (ViewHolder)convertView.getTag();
		}
		//设置数据
		Map<String,Object> rowData =  (Map<String, Object>)getItem(position);
		//异步加载图片防止错位方法一：com.android.volley.toolbox.ImageLoader
//		ImageLoader mImageLoader = MApplication.getImageLoader();
//		ImageListener mImageListener = mImageLoader.getImageListener(mViewHolder.iv_icon, R.drawable.default_icon, R.drawable.ic_launcher);
//		mImageLoader.get((String)rowData.get("imageUrl"), mImageListener);
		
		//异步加载图片防止错位方法二：com.nostra13.universalimageloader.core.ImageLoader
		
		universalimageloader.displayImage((String)rowData.get("imageUrl"), mViewHolder.iv_icon, ImageUtils.getFadeOptions(R.drawable.default_icon,R.drawable.ic_launcher,R.drawable.ic_launcher));
		mViewHolder.tv_title.setText((String)rowData.get("title"));
		return convertView;
	}

	class ViewHolder{
		ImageView iv_icon;
		TextView tv_title;
	}
	
	public static void setUniversalimageloader(ImageLoader imageloader)
	{
		universalimageloader = imageloader;
	}

}
